/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.netty.model;

import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 请求应答对象
 */
public class SentBody implements Serializable,Transportable {

    private static final long serialVersionUID = 1L;

    /**
     * 请求key
     */
    private String key;
    /**
     * 消息类型 心跳检测(0,1) 消息发送 2 ，上线 3 下线 4
     */
    private Integer type;

    /**
     * 返回说明
     */
    private String message;

    /**
     * 返回数据集合
     */
    private HashMap<String, Object> data;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public void put(String k, Object v) {
        if (v != null && k != null) {
            if(Objects.isNull(data)){
                data = new HashMap<>();
            }
            data.put(k, v);
        }
    }
    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public void putAll(Map<String, Object> map) {
        if(Objects.isNull(data)){
            data = new HashMap<>();
        }
        data.putAll(map);
    }

    public Object get(String k) {
        return data.get(k);
    }

    public void remove(String k) {
        data.remove(k);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Set<String> getKeySet() {
        return data.keySet();
    }


    @Override
    public String toString() {
        Map<String,Object> map = new HashMap<>();
        map.put("key",key);
        map.put("type",type);
        map.put("message",message);
        map.put("data",data);
        return JSONObject.toJSONString(map);
    }

    @Override
    public byte[] getBody() {
        return this.toString().getBytes();
    }

}
